//
//  WLTabBar.m
//  WLTabBar
//
//  Created by tanxl on 2019/12/29.
//  Copyright © 2019 tanxl. All rights reserved.
//

#import "WLTabBar.h"
#import "UIView+WLCore.h"
@interface WLTabBar ()<WLTabBarItemDelegate>
@property(strong,nonatomic) UIView *bg_view;
@property(strong,nonatomic) UIView *shadow_view;
@end

@implementation WLTabBar
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:self.shadow_view];
        [self addSubview:self.bg_view];
    }
    return self;
}
- (NSMutableArray <WLTabBarItem *>*)tabBarItems {
    if (_tabBarItems == nil) {
        _tabBarItems = [[NSMutableArray alloc] init];
    }
    return _tabBarItems;
}

- (void)addTabBarItem:(UITabBarItem *)item index:(NSInteger)index isEnd:(BOOL)isEnd {
    WLTabBarItem *tabBarItem = [[WLTabBarItem alloc] initWithType:WLTabBarItemTypeNormal];
    tabBarItem.delegate = self;
    tabBarItem.badgeTitleFont         = self.badgeTitleFont?:[UIFont systemFontOfSize:10.f];
    tabBarItem.itemTitleFont          = self.itemTitleFont?:[UIFont systemFontOfSize:10.f];
    tabBarItem.itemTitleColor         = self.itemTitleColor?:UIColor.darkGrayColor;
    tabBarItem.selectedItemTitleColor = self.itemSelectedTitleColor?:UIColor.blackColor;;
    // 进行关联
    tabBarItem.tabBarItem = item;
    
    tabBarItem.tag = index;
    
    [self addSubview:tabBarItem];
    [tabBarItem mas_makeConstraints:^(MASConstraintMaker *make) {
        if (index != 0){//不是第一个Item
            WLTabBarItem *topTabBarItem = [self.tabBarItems objectAtIndex:index-1];
            make.size.mas_equalTo(topTabBarItem);
            make.left.mas_equalTo(topTabBarItem.mas_right);
        } else {//第一个Item
            make.left.mas_equalTo(self);
        }
        if (isEnd) {//最后一个Item
            make.right.mas_equalTo(self);
        }
        make.top.bottom.mas_equalTo(self);
    }];
    [self.tabBarItems addObject:tabBarItem];

    if (self.tabBarItems.count == 1) {
        [self onClickTabBarItem:tabBarItem];
    }
}

- (void)onClickTabBarItem:(WLTabBarItem *)tabBarItem {
    BOOL isTrue = true;
    if (self.delegate && [self.delegate respondsToSelector:@selector(tabBar:didSelectedItemFrom:to:)]) {
        isTrue = [self.delegate tabBar:self didSelectedItemFrom:self.selectedItem.tag to:tabBarItem.tag];
    }
    if (isTrue){
        self.selectedItem.selected = NO;
        self.selectedItem = tabBarItem;
        self.selectedItem.selected = YES;
    }
}
- (void)layoutSubviews {
    [super layoutSubviews];
}
-(UIView *)shadow_view{
    if (!_shadow_view){
        _shadow_view = [[UIView alloc]initWithFrame:self.bounds];
        _shadow_view.backgroundColor = UIColor.whiteColor;
    }
    return _shadow_view;
}
-(UIView *)bg_view{
    if (!_bg_view){
        _bg_view = [[UIView alloc]initWithFrame:self.bounds];
        _bg_view.backgroundColor = UIColor.whiteColor;
    }
    return _bg_view;
}
@end
